*****************************************************************
*********** Mining and violent competition in Africa ************
****** A.S. Rigterink, T. Ghani, J.S. Lozano, J.N. Shapiro ******
******* Replication of Figures A3 and A4 of the Appendix ********

/*
0. Setting globals
1. Reading in IPIS data
2. Calculate closest mine in same wave and province

*/
*****************************************************************

clear all
version 14.0
set more off
cap log close

/*--------------------------------
------- 0. Setting globals -------
----------------------------------*/

*** Directory globals
*** NB: SET YOUR DIRECTORY HERE AND UNTEXT IF NOT RUNNING 0-Master.do
*global dir = ".\Stata"
*cd "$dir"

*** Subdirectories
global Do_files     "./Do"
global Data			"./Data"
global Results		"./Results"
global Tables 		"./Tables"
global Figures		"./Figures"

*** Scripts for spatially clustered standard errors
do "$Do_files/my_ols_spatial_HAC.do"
do "$Do_files/my_reg2hdfespatial.ado"

/*------------------------------------
------- 1. Reading in IPIS data  -----
--------------------------------------*/

insheet using "$Data\cod_mines_curated_all_opendata_p_ipis.csv", clear

* Identifying survey rounds and generating id 
gen date = date(visit_date, "YMD")

bysort project: egen date_avg = mean(date)
sort date_avg
egen project_id = group(date_avg)

/*-----------------------------------------------------
------- 2. Closest mine in same wave and province -----
-------------------------------------------------------*/

* Mines labelled in "Katanga" in 2017 and 2018: Katanga did not exist then
* Keep for now, drop later
replace province="Tanganyika" if province=="Katanga"

levelsof province, local(provinces)
levelsof project_id, local(waves)
local N = _N

foreach w in `waves' {
	
	di `w'
	
	gen dist`w'=999999999999999 if project_id<=`w'
	
		forvalues i = 1(1)`N' {
			if project_id[`i'] <=`w' {
			
			local lat1 = latitude[`i']
			local long1 = longitude[`i']
				
				qui geodist latitude longitude `lat1' `long1' , gen(temp)

				replace dist`w' = temp if temp<dist`w' & project_id<=`w' & cid!=cid[`i'] & province==province[`i']
							
				drop temp
			}
		}
}



/*-----------------------
------- 3. Graph A4 -----
-------------------------*/

drop if province_old=="Tanganyika"

* Dropping single mine in Haut-Huele
drop if province=="Haut-Uele"

tab project_id 
tab project

collapse (mean) dist? dist??, by(province)
gen id = _n
reshape long dist, i(id) j(wave)

* Ignoring nine mines in Bas-Uele
graph twoway (connected dist wave if province=="Haut-Katanga") /// 
|| (connected dist wave if province=="Haut-Lomami") ///
|| (connected dist wave if province=="Ituri") ///
|| (connected dist wave if province=="Maniema") ///
|| (connected dist wave if province=="Nord-Kivu") ///
|| (connected dist wave if province=="Sud-Kivu") ///
|| (connected dist wave if province=="Tanganyika") ///
|| (connected dist wave if province=="Tshopo" & wave!=2 & wave!=3) ///
|| (connected dist wave if province=="Lualaba"), /// 
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) ///
legend(row(3) order(1 "Haut-Katanga" 2 "Haut-Lomami" 3 "Ituri"  4 "Maniema" 5 "Nord-Kivu" 6 "Sud-Kivu" 7 "Tanganyika" 8 "Tshopo" 9 "Lualaba")) ///
ytitle("Distance to nearest known mine (km)") xtitle(IPIS data collection wave) xlabel(1(1)11)

graph export "$Figures/IPIS_dist.pdf", replace

/*-----------------------
------- 3. Graph A3 -----
-------------------------*/

collapse (mean) dist, by(wave)

graph twoway connected dist wave, /// 
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) ///
mcolor(black) lcolor(black) ///
ytitle("Distance to nearest known mine (km)") xtitle(IPIS data collection wave) xlabel(1(1)11)

graph export "$Figures/IPIS_dist_all.pdf", replace